CREATE PROCEDURE [dbo].[Sp_GoodsSupportiveCompanies] @Id NUMERIC(18,0) AS 
BEGIN 
DECLARE @TransactionRow AS TABLE
(
   Id NUMERIC(19,0),
   TransactionId SMALLINT,
   TransactionName NVARCHAR(30),
   TransactionPrice MONEY,
   TransactionPercent REAL,
   TransactionType TINYINT
)

INSERT INTO @TransactionRow(Id,TransactionId,  TransactionName, TransactionPrice, TransactionPercent, TransactionType)
SELECT Autoid, IncrementalDeductionId, Name, ISNULL(PriceSupportive,0) + ISNULL(DifferSupportive,0) , ROUND((ISNULL(PriceSupportive,0) + ISNULL(DifferSupportive,0))*100/Query.Tot_forosh,2) ,  Type FROM ( 
SELECT TmpDrugHavaleh.Autoid, IncrementalDeductionId, Name,  
CASE
    WHEN PatientPercent > 0 AND BimarPercent <> 100 THEN 
	                                                  
                                                        CASE
                                                           WHEN BimarPercent = 0 AND  Status =0 THEN Tot_forosh * PatientPercent/100
														   WHEN BimarPercent = 0 AND  Status IN(4,5,6) THEN 0   
														   ELSE Tot_forosh * BimarPercent/100* PatientPercent/100
														END

   WHEN PatientPrice > 0 AND BimarPercent <> 100 THEN                                                          
                                                        CASE
                                                           WHEN BimarPercent = 0 AND Status =0 AND Tot_forosh < PatientPrice * K_Qty1 THEN Tot_forosh
														   WHEN BimarPercent = 0 AND Status =0 AND  Tot_forosh >= PatientPrice * K_Qty1 THEN PatientPrice * K_Qty1
                                                           WHEN BimarPercent = 0 AND Status IN(4,5,6) THEN 0
														   WHEN Tot_forosh * BimarPercent/100 > PatientPrice * K_Qty1 THEN PatientPrice * K_Qty1
														   ELSE ROUND(Tot_forosh * BimarPercent/100,0)
														END 
  END AS PriceSupportive,
  CASE
  WHEN DifferPercent > 0 AND Tot_Differ >0 THEN 
                                                        CASE
                                                           WHEN DifferPercent/100 * Tot_Differ  >= Tot_Differ THEN  Tot_Differ
														   ELSE DifferPercent * Tot_Differ/100
														END
  WHEN DifferPrice > 0 AND Tot_Differ >0 THEN                                                          
                                                        CASE
                                                           WHEN DifferPrice*K_Qty1 <= Tot_Differ THEN DifferPrice *K_Qty1
														   ELSE Tot_Differ
														END 
  END AS DifferSupportive,
  Type, Tot_forosh
  FROM dbo.TmpDrugHavaleh 
  INNER JOIN GoodsSupportiveCompanies ON GoodsCode = K_Code
  INNER JOIN dbo.Over_Under ON Over_Under.AutoId = dbo.GoodsSupportiveCompanies.IncrementalDeductionId
  WHERE Id_Havaleh = @Id AND Status NOT IN(1,3,8)
AND 
Status > 
CASE  
  WHEN Kind = 0 THEN -1
  ELSE 0
END) AS Query

INSERT INTO dbo.Tmp_Over_Under_Row_Drug(PrescriptionId,Code_Over_Under, Price, [Percent], Name, Type)
SELECT Id, TransactionId , TransactionPrice, 0, TransactionName,  TransactionType  FROM @TransactionRow
WHERE NOT EXISTS(SELECT PrescriptionId FROM dbo.Tmp_Over_Under_Row_Drug WHERE TransactionId = Code_Over_Under 
                 AND PrescriptionId = [@TransactionRow].Id
				 )

UPDATE dbo.Tmp_Over_Under_Row_Drug SET price = [@TransactionRow].TransactionPrice, [Percent] = [@TransactionRow].TransactionPercent FROM dbo.Tmp_Over_Under_Row_Drug
INNER JOIN @TransactionRow ON Id = dbo.Tmp_Over_Under_Row_Drug.PrescriptionId AND TransactionId = Code_Over_Under

END
